﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Xml;
using System.Net.Sockets;

namespace GameAiLaTrieuPhu_Server
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Server server;
        public XmlDocument xmlDoc;
        private int nPlayer;
        // isReady:
        //          True: If admin chose N player
        //          False: Else
        private bool isReady; 

        public MainWindow()
        {
            InitializeComponent();

            isReady = false;
            // Hide Bar
            HideSystemBar();
            HideQuestionBar();
            HidePlayerBar();

            // Constructor
            xmlDoc = new XmlDocument();
            xmlDoc.Load("./Data/ListQuestions.xml");
        }

        // Process Mouse -------------------------------------------------------------------------------------------------
		public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        public const int MAX_PLAYER = 10;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg,
                int wParam, int lParam);

        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();
		
        private void MoveWindow(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			ReleaseCapture();
            SendMessage(new WindowInteropHelper(this).Handle,
                WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
		
		private void EXIT(object sender, MouseButtonEventArgs e)
        {
            Environment.Exit(0);
        }

        private void MINIMIZE(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void ActivateTitleIcons(object sender, MouseEventArgs e)
        {
            btnClose.Fill = (ImageBrush)Main.Resources["ButtonClose02"];
            btnMinimize.Fill = (ImageBrush)Main.Resources["ButtonMinimize02"];
        }

        private void DeactivateTitleIcons(object sender, MouseEventArgs e)
        {
            btnClose.Fill = (ImageBrush)Main.Resources["ButtonClose01"];
            btnMinimize.Fill = (ImageBrush)Main.Resources["ButtonMinimize01"];
        }

        private void PressingButtonClose(object sender, MouseButtonEventArgs e)
        {
            btnClose.Fill = (ImageBrush)Main.Resources["ButtonClose03"];
        }

        private void PressingButtonMinimize(object sender, MouseButtonEventArgs e)
        {
            btnMinimize.Fill = (ImageBrush)Main.Resources["ButtonMinimize03"];
        }

        private void ActivateTitleIconsButtonStart(object sender, System.Windows.Input.MouseEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			btnStart.Fill = (ImageBrush)Main.Resources["ButtonStart02"];
        }

        private void DeactivateTitleIconsButtonStart(object sender, System.Windows.Input.MouseEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			btnStart.Fill = (ImageBrush)Main.Resources["ButtonStart01"];
        }

        private void PressingButtonStart(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			btnStart.Fill = (ImageBrush)Main.Resources["ButtonStart03"];
        }

        private void ActivateTitleIconsButtonExit(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            btnExit.Fill = (ImageBrush)Main.Resources["ButtonExit02"];
        }

        private void DeactivateTitleIconsButtonExit(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            btnExit.Fill = (ImageBrush)Main.Resources["ButtonExit01"];
        }

        private void PressingButtonExit(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            btnExit.Fill = (ImageBrush)Main.Resources["ButtonExit03"];
        }

        private void ExitGame(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            EXIT(sender, e);
        }

        // Process Bar ------------------------------------------------------------------------------------------------
        private void ShowSystemBar()
        {
            // Show System Bar                       
            recMessage.Visibility = Visibility;
            tblChonSoLuongPlayer.Visibility = Visibility;
            tbxNumberOfPlayer.Visibility = Visibility;            
            tblThongBaoGame.Visibility = Visibility;
            tblNoticeContent.Visibility = Visibility;
            btnManageGame.Visibility = Visibility;
        }

        private void ShowQuestionBar()
        {
            // Show Question Bar
            recQuestionBorder.Visibility = Visibility;
            recAnswerBorder.Visibility = Visibility;
            tblThongTinCauHoi.Visibility = Visibility;
            tblSTTCauHoi.Visibility = Visibility;
            tblNumberQuestion.Visibility = Visibility;
            tblTongCauHoi.Visibility = Visibility;
            tblTotalQuestion.Visibility = Visibility;
            tblNoiDungCauHoi.Visibility = Visibility;
            tblQuestionContent.Visibility = Visibility;
            tblDapAnCauHoi.Visibility = Visibility;
            tblAnswerContent.Visibility = Visibility;
        }

        private void ShowPlayerBar()
        {
            // Show Player Bar
            recAnswerPlayerBorder.Visibility = Visibility;
            tblThongTinNguoiChoi.Visibility = Visibility;
            tblNicknameNguoiChoi.Visibility = Visibility;
            tblNicknamePlayer.Visibility = Visibility;
            tblAnswerPlayer.Visibility = Visibility;
            tblCauTraLoi.Visibility = Visibility;
            tblSTTNguoiChoi.Visibility = Visibility;
            tblNumberPlayer.Visibility = Visibility;
            tblSoNguoiChoiConLai.Visibility = Visibility;
            tblNumberOfPossiblePlayer.Visibility = Visibility;
            tblThongBao.Visibility = Visibility;
            tblNoticePlayer.Visibility = Visibility;
        }

        private void HideSystemBar()
        {
            // Hide System Bar                       
            recMessage.Visibility = Visibility.Hidden;
            tblChonSoLuongPlayer.Visibility = Visibility.Hidden;
            tbxNumberOfPlayer.Visibility = Visibility.Hidden;
            tblSoLuongPlayerDangKy.Visibility = Visibility.Hidden;
            tblNumberOfPlayerRegistry.Visibility = Visibility.Hidden;
            tblThongBaoGame.Visibility = Visibility.Hidden;
            tblNoticeContent.Visibility = Visibility.Hidden;
            btnManageGame.Visibility = Visibility.Hidden;
        }

        private void HideQuestionBar()
        {
            // Hide Question Bar
            recQuestionBorder.Visibility = Visibility.Hidden;
            recAnswerBorder.Visibility = Visibility.Hidden;
            tblThongTinCauHoi.Visibility = Visibility.Hidden;
            tblSTTCauHoi.Visibility = Visibility.Hidden;
            tblNumberQuestion.Visibility = Visibility.Hidden;
            tblTongCauHoi.Visibility = Visibility.Hidden;
            tblTotalQuestion.Visibility = Visibility.Hidden;
            tblNoiDungCauHoi.Visibility = Visibility.Hidden;
            tblQuestionContent.Visibility = Visibility.Hidden;
            tblDapAnCauHoi.Visibility = Visibility.Hidden;
            tblAnswerContent.Visibility = Visibility.Hidden;
        }

        private void HidePlayerBar()
        {
            // Hide Player Bar
            recAnswerPlayerBorder.Visibility = Visibility.Hidden;
            tblThongTinNguoiChoi.Visibility = Visibility.Hidden;
            tblNicknameNguoiChoi.Visibility = Visibility.Hidden;
            tblNicknamePlayer.Visibility = Visibility.Hidden;
            tblAnswerPlayer.Visibility = Visibility.Hidden;
            tblCauTraLoi.Visibility = Visibility.Hidden;
            tblSTTNguoiChoi.Visibility = Visibility.Hidden;
            tblNumberPlayer.Visibility = Visibility.Hidden;
            tblSoNguoiChoiConLai.Visibility = Visibility.Hidden;
            tblNumberOfPossiblePlayer.Visibility = Visibility.Hidden;
            tblNoticePlayer.Visibility = Visibility.Hidden;
            tblThongBao.Visibility = Visibility.Hidden;
        }

        private void ResetSystemBar()
        {
            // Reset System Bar                       
            tbxNumberOfPlayer.Text = "";
            tblNumberOfPlayerRegistry.Text = "0";
            tblNoticeContent.Text = "";
            btnManageGame.Content = "OK";
        }

        private void ResetQuestionBar()
        {
            // Reset Question Bar            
            tblNumberQuestion.Text = "0";            
            tblTotalQuestion.Text = "0";            
            tblQuestionContent.Text= "";            
            tblAnswerContent.Text = "";
        }

        private void ResetPlayerBar()
        {
            // Reset Player Bar            
            tblNicknamePlayer.Text = "";
            tblAnswerPlayer.Text = "";
            tblNumberPlayer.Text = "0";
            tblNumberOfPossiblePlayer.Text = "0";
            tblNoticePlayer.Text = "";
        }

        // Process Business -------------------------------------------------------------------------------------------------
        private void NewGame(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            // Reset some object
            isReady = false;
            // Reset Bar
            ResetSystemBar();
            ResetQuestionBar();
            ResetPlayerBar();
            
            // Hide Bar
            HideSystemBar();
            HideQuestionBar();
            HidePlayerBar();
            
            // Show Bar
            ShowSystemBar();
        }        

        private void btnManageGame_Click(object sender, RoutedEventArgs e)
        {
            if (isReady)
            {
                if (server.nPlayers == server.NPLAYERS)
                {
                }
                else
                {
                    tblNoticeContent.Text = "Không thể bắt đầu trò chơi khi chưa đủ Người chơi!";
                }
            }
            else
            {
                try
                {
                    int iNumberOfPlayer = int.Parse(tbxNumberOfPlayer.Text);
                    if (iNumberOfPlayer < 1 || iNumberOfPlayer > MAX_PLAYER)
                    {
                        tblNoticeContent.Text = "Số lượng Người chơi phải là một số tự nhiên từ 1 đến 10.";
                    }
                    else
                    {
                        // Get number of player
                        nPlayer = iNumberOfPlayer;
                        tblNoticeContent.Text = "Thiết lập thành công Hệ thống câu hỏi.\nChờ Người chơi đăng nhập.";
                        // Hide some controls
                        tblChonSoLuongPlayer.Visibility = Visibility.Hidden;
                        tbxNumberOfPlayer.Visibility = Visibility.Hidden;
                        tblSoLuongPlayerDangKy.Visibility = Visibility;
                        tblNumberOfPlayerRegistry.Visibility = Visibility;
                        // Rename Button Manage Game
                        btnManageGame.Content = "Bắt đầu cuộc chơi";
                        // Turn on flag isReady
                        isReady = true;
                        // Constructor and Init server
                        XmlNode xmlNodeQUESTION = xmlDoc.SelectSingleNode("/LIST_QUESTIONS");
                        server = new Server(xmlNodeQUESTION, nPlayer);
                        server.Start();
                    }
                }
                catch
                {
                    tblNoticeContent.Text = "Số lượng Người chơi phải là một số tự nhiên từ 1 đến " + MAX_PLAYER.ToString();
                }
            }
        }

        private void StartGame()
        {
            tblTotalQuestion.Text = server.nQuestions.ToString();
            tblNumberPlayer.Text = server.iPlayerNumber.ToString();
            tblNumberQuestion.Text = server.iQuestionNumber.ToString();
            tblNumberOfPossiblePlayer.Text = server.NPLAYERS.ToString();
            tblQuestionContent.Text = server.GetQuestionContent();
            tblAnswerContent.Text = server.GetAnswer();

            ShowQuestionBar();
            ShowPlayerBar();
        }
    }
}
